package cn.butcher21;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.math.BigDecimal;

public class MainActivity extends AppCompatActivity {

    private EditText text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 算式
        text = findViewById(R.id.text);

        // 归零按钮
        findViewById(R.id.returnToZero).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text.setText("0");
                text.setSelection(text.getText().toString().length());
            }
        });

        //左括号
        findViewById(R.id.leftBracket).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (s.equals("0")) {
                    text.setText("(");
                } else {
                    text.setText(s + "(");
                }
                text.setSelection(text.getText().toString().length());

            }
        });

        // 右括号
        findViewById(R.id.rightBracket).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (!s.equals("0")) {
                    text.setText(s + ")");
                    text.setSelection(text.getText().toString().length());
                }
            }
        });

        // 删除最后一位
        findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (s.length() == 1) {
                    text.setText("0");
                    return;
                }
                text.setText(s.substring(0, s.length() - 1));
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.sin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (!s.equals("0")) {
                    try {
                        BigDecimal result = Calculator.getResult(s);
                        if (result.intValue() == 180  ||result.intValue() == -180 ||result.intValue() == 360 || result.intValue() == -360 ){
                            text.setText( "0");
                        }else {
                            double sin = Math.sin(result.doubleValue()*Math.PI/180);
                            text.setText( sin+"");
                        }

                    } catch (BracketErrorException e) {
                        Toast toast = Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT);
                        toast.show();
                    } catch (NumberFormatException e) {
                        Toast toast = Toast.makeText(getApplicationContext(), "算式格式错误！", Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }
            }
        });

        findViewById(R.id.cos).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                    try {
                        BigDecimal result = Calculator.getResult(s);
                        if (result.intValue() == 90 ||result.intValue() == 270 || result.intValue() == -90 || result.intValue() == -270 ){
                            text.setText("0");
                        }else {
                            text.setText( Math.cos(Math.toRadians(result.doubleValue()))+"");
                        }

                    } catch (BracketErrorException e) {
                        Toast toast = Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT);
                        toast.show();
                    } catch (NumberFormatException e) {
                        Toast toast = Toast.makeText(getApplicationContext(), "算式格式错误！", Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }
        });

        findViewById(R.id.tan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (!s.equals("0")) {
                    try {
                        BigDecimal result = Calculator.getResult(s);

                        if (result.intValue() == 180  ||result.intValue() == -180 ||result.intValue() == 360 ||result.intValue() == -360 ){
                            text.setText("0");
                        } else if (result.doubleValue() == 45.0 || result.doubleValue() == 225.0 ){
                            text.setText("1");
                        } else if (result.doubleValue() == -45.0 || result.doubleValue() == -225.0){
                            text.setText("-1");
                        }else if (result.doubleValue() == 90 || result.doubleValue() == 270 || result.doubleValue() == -90 || result.doubleValue() == -270){
                            Toast toast = Toast.makeText(getApplicationContext(), "tan90°不存在！", Toast.LENGTH_SHORT);
                            toast.show();
                        }else {
                            text.setText( Math.tan(Math.toRadians(result.doubleValue()))+"");
                        }
                    } catch (BracketErrorException e) {
                        Toast toast = Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT);
                        toast.show();
                    } catch (NumberFormatException e) {
                        Toast toast = Toast.makeText(getApplicationContext(), "算式格式错误！", Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }
            }
        });

        findViewById(R.id.surplus).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text.setText(text.getText().toString() + "%");
                text.setSelection(text.getText().toString().length());
            }
        });


        // 1/x
        findViewById(R.id.fraction).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (!s.equals("0")) {
                    try {
                        BigDecimal result = Calculator.getResult(s);
                        BigDecimal divide = new BigDecimal(1).divide(result);
                        text.setText( divide.toString());
                    } catch (BracketErrorException e) {
                        Toast toast = Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT);
                        toast.show();
                    } catch (NumberFormatException e) {
                        Toast toast = Toast.makeText(getApplicationContext(), "算式格式错误！", Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }
            }
        });

        findViewById(R.id.square).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (!s.equals("0")) {
                    try {
                        BigDecimal result = Calculator.getResult(s);
                        text.setText((result.pow(2)) + "");
                    } catch (BracketErrorException e) {
                        Toast toast = Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT);
                        toast.show();
                    } catch (NumberFormatException e) {
                        Toast toast = Toast.makeText(getApplicationContext(), "算式格式错误！", Toast.LENGTH_SHORT);
                        toast.show();
                    }

                }
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.prescription).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();

                try {
                    if (!s.equals("0")){
                        BigDecimal result = Calculator.getResult(s);
                        text.setText(Math.sqrt(result.doubleValue()) + "");
                    }

                    text.setSelection(text.getText().toString().length());
                } catch (Exception e) {
                    Toast toast = Toast.makeText(getApplicationContext(), "算式格式错误！", Toast.LENGTH_SHORT);
                    toast.show();
                }

            }
        });

        findViewById(R.id.divide).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text.setText(text.getText().toString() + "÷");
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.one).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (s.equals("0")) {
                    text.setText("1");
                } else {
                    text.setText(s + "1");
                }
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.tow).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (s.equals("0")) {
                    text.setText("2");
                } else {
                    text.setText(s + "2");
                }
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.three).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (s.equals("0")) {
                    text.setText("3");
                } else {
                    text.setText(s + "3");
                }
                text.setSelection(text.getText().toString().length());
            }
        });


        findViewById(R.id.multiply).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                text.setText(s + "x");
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.four).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (s.equals("0")) {
                    text.setText("4");
                } else {
                    text.setText(s + "4");
                }
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.five).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (s.equals("0")) {
                    text.setText("5");
                } else {
                    text.setText(s + "5");
                }
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.six).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (s.equals("0")) {
                    text.setText("6");
                } else {
                    text.setText(s + "6");
                }
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.subtract).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                text.setText(s + "-");
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.seven).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (s.equals("0")) {
                    text.setText("7");
                } else {
                    text.setText(s + "7");
                }
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.eight).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (s.equals("0")) {
                    text.setText("8");
                } else {
                    text.setText(s + "8");
                }
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.nine).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (s.equals("0")) {
                    text.setText("9");
                } else {
                    text.setText(s + "9");
                }
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                text.setText(s + "+");
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.minusSign).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (s.equals("0")) {
                    text.setText("-");
                } else {
                    text.setText(s + "-");
                }
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.zero).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                if (s.equals("0")) {
                    text.setText("0");
                } else {
                    text.setText(s + "0");
                }
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.spot).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();
                text.setText(s + ".");
                text.setSelection(text.getText().toString().length());
            }
        });

        findViewById(R.id.equal).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString();

                try {
                    ;
                    text.setText(Calculator.getResult(s).toString());
                } catch (BracketErrorException e) {
                    Toast toast = Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT);
                    toast.show();
                } catch (NumberFormatException e) {
                    Toast toast = Toast.makeText(getApplicationContext(), "算式格式错误！", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });

    }
}
